package com.mcptt.notify.xcap;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.ue.initial.UeInitProfile;

public class XcapParser
{
	public static final String Key = "<xcap-diff xmlns=\"";
	private static final String Name = "xcap-diff";
	private static final String Name_Root = "xcap-root";

//	private String root = null;
	private List<XcapDoc> docList = null;


	public void parse(String xmlStr) throws IOException,JDOMException
	{
		Log.debug(this.getClass(), "parse xmlStr = " + xmlStr);
		SAXBuilder builder = new SAXBuilder();//实锟斤拷JDOM锟斤拷锟斤拷锟斤拷  
		//	File file = new File(Environment.getExternalStorageDirectory()+FilePath,FileName);
		//	FileInputStream fis = new FileInputStream(file);
		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		Document document = builder.build(strIs);
		Element rootEle = document.getRootElement();//锟斤拷酶锟斤拷诘锟�<bookstore> 
		if(rootEle == null)
		{
			return;
		}
//		root = rootEle.getAttributeValue(Name_Root);
		docList = new Vector<XcapDoc>();

		List<Element> list = rootEle.getChildren();//锟斤拷酶锟斤拷诘锟斤拷锟接节碉拷  
		for(Element elem:list) 
		{
			String name = elem.getName();
			Log.debug(this.getClass(), "name = " + name);
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			if(name.equals(XcapDoc.Name))
			{
				XcapDoc doc = new XcapDoc();
				doc.parse(elem);
				docList.add(doc);
			}
		}  
	}

//	public String getRoot()
//	{
//		return root;
//	}

	public List<XcapDoc> getDocList()
	{
		return docList;
	}
	//    public List<String> getXCapPathList()
	//    {
	//	List<String> xCapPathList = new Vector<String>();
	//	String rootPath = getRoot();
	//	for(Iterator<XcapDoc> itor = docList.iterator();itor.hasNext();)
	//	{
	//	    String subPath = itor.next().getSel();
	//	    xCapPathList.add(rootPath + subPath);
	//	}
	//	return xCapPathList;
	//    }

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuf.append("<").append(Name).append(" xmlns=\"urn:ietf:params:xml:ns:xcap-diff\" ");
//		strBuf.append(Name_Root).append("=\"").append(root).append("\" >");
		for(Iterator<XcapDoc> itor = docList.iterator();itor.hasNext();)
		{
			strBuf.append(itor.next());
		}
		strBuf.append("</").append(Name).append(">");

		return strBuf.toString();
	}
}
